package com.lhh.jiu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhh.jiu.data.Result;
import com.lhh.jiu.entity.Admin;
import com.lhh.jiu.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("管理人员")
@Controller
public class AdminApiController {

    @Autowired
    private AdminService adminService;

    //查询全部数据
    @ApiOperation("获取所有信息，不分页")
    @GetMapping("/admin")
    @ResponseBody
    public Object getAll(){
        List<Admin> list = adminService.list();
        return Result.getHashMapList(list);
    }

    //分页查询，带有条件
    @ApiOperation("分页查询，带有参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "用户姓名",paramType = "query"),
            @ApiImplicitParam(name="wtime",value = "工作时间",paramType = "query"),
    })
    @GetMapping("/adminPage")
    @ResponseBody
    public Object AdminPage(@RequestParam(value = "name",required = false)String name
            ,@RequestParam(value = "wtime",required = false)String wtime
            ,@RequestParam(defaultValue = "1")int pageNum
            ,@RequestParam(defaultValue = "3")int pageSize){
        try {
            IPage<Admin> iPage = adminService.selectAdmin(pageNum,pageSize,name,wtime);
            return Result.getHashMapMysqlPage(iPage);
        }catch (Exception e){
            return Result.getHashMapException(e);
        }
    }

    //添加一个数据
    @ApiOperation("添加或者更新一个数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",paramType = "query"),
            @ApiImplicitParam(name="name",value = "用户姓名",paramType = "query"),
            @ApiImplicitParam(name="phone",value = "联系方式",paramType = "query"),
            @ApiImplicitParam(name="wtime",value = "工作时间",paramType = "query"),
    })
    @PostMapping("/admin")
    @ResponseBody
    public Object adminAdd(@RequestParam(value = "id",required = false)Integer id
            ,@RequestParam(value = "name",required = false)String name
            ,@RequestParam(value = "phone",required = false)String phone
            ,@RequestParam(value = "wtime",required = false) String wtime){
        Admin admin= new Admin();
        admin.setId(id);
        admin.setName(name);
        admin.setPhone(phone);
        admin.setWtime(wtime);
        try {
            //添加数据，会返回一个数据，查看是否插入成功
            adminService.AddOrUpdate(admin);
            return Result.getHashMapSave(1);
        }catch (Exception e){
            return Result.getHashMapException(e);
        }
    }

    //删除一个数据
    @ApiOperation("通过id删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "主键",paramType = "query",required = true)
    })
    @DeleteMapping("/admin")
    @ResponseBody
    public Object adminDelById(Integer id){
        try {
            int i = adminService.delAdmin(id);
            return Result.getHashMapDel(i);
        }catch (Exception e){
            return Result.getHashMapException(e);
        }
    }

    //带条件查询，不分页
    @ApiOperation("带条件查询，不分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "用户姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name="wtime",value = "工作时间",paramType = "query",dataType = "String")
    })
    @GetMapping("/selectNT")
    @ResponseBody
    public Object selectNameAndTime(@RequestParam(value = "name",required = false)String name,@RequestParam(value = "wtime",required = false)String wtime){
        try {
            List<Admin> list = adminService.queryNameAndTime(name,wtime);
            return Result.getHashMapList(list);
        }catch (Exception e){
            return Result.getHashMapException(e);
        }
    }

}
